#!/usr/bin/env python
# coding=utf-8
# 15/4/12 20:47 zhuxuehuang

import datetime

# n = datetime.datetime.now()
# print(n)
#
# dt_now = datetime.date.today()
# print(dt_now)
# print(dt_now.year)
#
# d1 = datetime.date(dt_now.year, dt_now.month, dt_now.day)
# print(d1.month)
#
# d2 = d1.replace(month=d1.month - 1)
# print(d2)
#
# # 昨天
# t1 = dt_now.replace(day=dt_now.day - 1)
# print(t1)

# # 前天
# t2 = dt_now.replace(day=dt_now.day - 2)
# print(t2)

import datetime
from datetime import timedelta
import calendar
import time


def getLastDayOfLastMonth(m):
    d = datetime.date(2015, m, 1)
    c = calendar.Calendar()

    year = d.year
    month = d.month

    if month == 1:
        month = 12
        year -= 1
    else:
        month -= 1
    print('year %s , month %s ' % (year, month))
    days = calendar.monthrange(year, month)[1]
    # print('days %d' % days)
    # print(calendar.monthrange(year, month))
    print(datetime.datetime(year, month, 1)).strftime('%Y-%m-%d %X')
    print(time.mktime(datetime.datetime(year, month, 1, 0, 0, 0).timetuple()))
    print(datetime.datetime(year, month, days)).strftime('%Y-%m-%d %X')
    print(time.mktime(datetime.datetime(year, month, days, 23, 59, 59).timetuple()))
    print('')


# for i in range(1, 13):
#     g = getLastDayOfLastMonth(i)


# 设a为字符串
# import time
#
a = "2015-04-12 10:00:00"
#中间过程，一般都需要将字符串转化为时间数组
tt1 = time.strptime(a, '%Y-%m-%d %H:%M:%S')
print(tt1)
#将"2011-09-28 10:00:00"转化为时间戳
tt2 = time.mktime(time.strptime(a, '%Y-%m-%d %H:%M:%S'))
print(tt2)
#将时间戳转化为localtime
x = time.localtime(1428804000.0)
tt3 = time.strftime('%Y-%m-%d %H:%M:%S %z %Z', x)
print(tt3)


print('=' * 10)
dd1 = datetime.datetime(2015, 4, 12, 10, 00, 00)
print(dd1)
dd2 = time.mktime(time.strptime(str(dd1), '%Y-%m-%d %H:%M:%S'))
print(dd2)
dd21 = time.localtime(dd2)
dd3 = time.strftime('%Y-%m-%d %H:%M:%S %z %Z', dd21)
print(dd3)

# from datetime import tzinfo
# import time
#
# class GMT1(tzinfo):
#     def utcoffset(self, dt):
#         return timedelta(hours=8)
#     def dst(self, dt):
#         return timedelta(0)
#     def tzname(self,dt):
#         return "Europe/Prague"
#
# t = datetime.time(12, 10, 30, tzinfo=GMT1())
# print(t) # t doctest: +ELLIPSIS
# # datetime.time(12, 10, 30, tzinfo=<GMT1 object at 0x...>)
# t.isoformat()
# print(t.isoformat())
# # '12:10:30+01:00'
# t.dst()
# datetime.timedelta(0)
# t.tzname()
# print(t.tzinfo)
# # 'Europe/Prague'
# t.strftime("%H:%M:%S %Z")
# print(t.strftime("%H:%M:%S %z %Z"))
# # '12:10:30 Europe/Prague'
#
# tt2 = time.mktime(t.strftime("%H:%M:%S"), '%H:%M:%S')
# print(tt2)

print('x' * 10)
for i in range(1, 13):
    print(datetime.date(2015, i, 1) - datetime.timedelta(1))